package com.smartTerminal.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 将汉字转换成拼音字符串
 * @project	smartTerminal
 * @author	Xia.Zhuming
 * @data	2018年1月9日
 */
public class ChineseConverUtil {

	/**
	 * 将汉字转换成拼音字符串
	 * @method	toLetter
	 * @author	Xia.Zhuming
	 * @data	2018年1月9日
	 * @param chinese
	 * @return
	 */
	public static String toLetter(String chinese){          
		String pinyinStr = "";  
		char[] newChar = chinese.toCharArray();  
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
		for (int i = 0; i < newChar.length; i++) {  
			if (newChar[i] > 128) {  
				try {  
					pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];  
				} catch (BadHanyuPinyinOutputFormatCombination e) {  
					e.printStackTrace();  
				}  
			}else{  
				pinyinStr += newChar[i];  
			}  
		}  
		return pinyinStr;  
	}  
}
